// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        hsbMaterial:cc.Material,

        redSlider:cc.Slider,
        greenSlider:cc.Slider,
        blueSlider:cc.Slider,
    },

    // LIFE-CYCLE CALLBACKS:
    ctor(){
        this.sp_dst = null;
        this.curMaterial = null;

        this.paramRed = 0;
        this.paramGreen = 0;
        this.paramBlue = 0;
    },

    initWithDstSprite(sp){
        this.sp_dst = sp;
        this.sp_dst.setMaterial(0,this.hsbMaterial);
        this.curMaterial = this.sp_dst.getMaterial(0);

        this.redSlider.progress = this.valueToProgress(this.paramRed);
        this.greenSlider.progress = this.valueToProgress(this.paramGreen);
        this.blueSlider.progress = this.valueToProgress(this.paramBlue);
        this._updateUniformValues();
    },

    _updateUniformValues(){
        // cc.log(`this.paramRed:${this.paramRed}`);
        // cc.log(`this.paramGreen:${this.paramGreen}`);
        // cc.log(`this.paramBlue:${this.paramBlue}`);
        this.curMaterial.setProperty('u_paramColor',{
            x:this.paramRed,
            y:this.paramGreen,
            z:this.paramBlue,
        });
    },

    progressToValue(progress){
        return 2 * progress - 1;
    },

    valueToProgress(value){
        return (value + 1) / 2;
    },

    onRedSliderChanged(){
        this.paramRed = this.progressToValue(this.redSlider.progress);
        this._updateUniformValues();
    },
    onGreenSliderChanged(){
        this.paramGreen = this.progressToValue(this.greenSlider.progress);
        this._updateUniformValues();
    },
    onBlueSliderChanged(){
        this.paramBlue = this.progressToValue(this.blueSlider.progress);
        this._updateUniformValues();
    },
});
